Skip to content

Add replace_last and remove_last filters#1422

Merged
dylanahsmith merged 24 commits intoShopify:masterfrom
andershagbard:master
Feb 24, 2022
Merged

Add replace_last and remove_last filters#1422
dylanahsmith merged 24 commits intoShopify:masterfrom
andershagbard:master

Conversation

@andershagbard
Copy link
Copy Markdown
Contributor

This PR adds two filters replace_last and remove_last. They do exactly what you think. The opposites of replace_first and remove_first.

Related issue: #778

Copy link
Copy Markdown
Contributor

@ADTC ADTC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some test cases need to be improved. 🙂

Copy link
Copy Markdown
Contributor

@ADTC ADTC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for considering 🙂

Co-Authored-By: ADTC <ADTC@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@ADTC ADTC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! 👍

@PaulNewton
Copy link
Copy Markdown

PaulNewton commented Apr 15, 2021

@andershagbard Thank you looking forward to the convenience and consistency this creates.

This filter will solve a category of string parsing problems needed in templates, it obsoletes several methods in addition to unintuitive negative slices (reverse-array, hard loops) similar to the examples below.

When this gets merged , or to figure out if it should be merged , performance minded providers may want to do static analysis for patterns similar to the following for optimization, refactoring or notifying consumers to update their logic.

String to reverse arrays

{%- comment -%}the splits and joins may occur immediately before
split and join filters parameter will vary(-,/,;,</p>) or be a passed variable `delimiter` set elsewhere {%- endcomment -%}
{%- assign value = "keyword" -%}
{%- assign string_with_value = "stringkeyword" -%}
{%- assign value_reversed =  value  | split: "" |  reverse |  join: "" -%}
{%- assign string_without_value = string_with_value | split: "" | reverse | join: "" | remove_first: value_reversed | split: "" | reverse | join: "" -%}

{%- comment -%} Sample output {%- endcomment -%}
value: {{ value }}
string_with_value: {{ string_with_value }}
value_reversed: {{ value_reversed }}
string_without_value: {{ string_without_value }}

Forloop using reverseindex

{%- assign value = "keyword"  -%}
{%- assign string_with_value = "stringkeyword" | split:"" -%}
{%- assign string_without_value = "" -%}

{%- for character in string_with_value -%}
{%- if forloop.rindex <= value.size -%}{%- continue -%}{%endif-%}
{%- assign string_without_value = string_without_value  | append:character -%}
{%- endfor -%}

{% comment %} Sample output {% endcomment %}
value: {{ value }}
string_with_value: {{ string_with_value }}
string_without_value : {{ string_without_value }}

Truncate method

{% assign value = "keyword" %}
{% assign string_with_value = "stringkeyword" %}
{% assign truncate_size = string_with_value.size | minus: value.size %}
{{ string_with_value | truncate:truncate_size,"" }}

Some use cases are fixing/checking/parsing html content from RTE's, or in the case of shopify things like interrelating objects by a keyword such as then end string of every product handle in a store.

@andershagbard
Copy link
Copy Markdown
Contributor Author

andershagbard commented Apr 16, 2021

I can't do these performance checks myself, but I believe simplicity should be in mind when deciding if this should get merged.

Copy link
Copy Markdown
Contributor

@dylanahsmith dylanahsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested some improvements, but having these corresponding filters does make sense to me.

andershagbard and others added 3 commits September 17, 2021 08:26
Co-authored-by: Dylan Thacker-Smith <dylan.smith@shopify.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants